package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.StaffRanking;
import com.xmy.cultivate.entity.StaffRankingDetail;
import com.xmy.cultivate.service.IStaffRankingDetailService;
import com.xmy.cultivate.service.IStaffRankingService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <排位>
 * 员工排位名称 前端控制器
 * </排位>
 *
 * @author hpiggy
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/api/staffRanking")
public class StaffRankingController {
    @Autowired
    IStaffRankingService iStaffRankingService;

    @Autowired
    IStaffRankingDetailService iStaffRankingDetailService;

    @PostMapping("")
    @ApiOperation("添加排位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "排位名次", required = false, dataType = "String"),
            @ApiImplicitParam(name = "rankingDate", value = "排位日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = false, dataType = "String"),
    })
    public Result saveRanking(@RequestBody StaffRanking staffRanking)  {
        boolean save = iStaffRankingService.save(staffRanking);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERROR, null, save ? Code.SAVE_OK_MSG : Code.SAVE_ERROR_MSG);
    }

    @PutMapping("")
    @ApiOperation("修改排位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "排位名次", required = false, dataType = "String"),
            @ApiImplicitParam(name = "rankingDate", value = "排位日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = false, dataType = "String"),
    })
    public Result updateRanking(@RequestBody StaffRanking staffRanking) {
        StaffRanking staffRanking1 = iStaffRankingService.getById(staffRanking.getId());

        if (staffRanking1 == null) {
            return new Result(Code.UPDATE_ERROR, null, "该排位不存在，修改失败");
        }

        boolean update = iStaffRankingService.updateById(staffRanking);
        return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERROR, null, update ? Code.UPDATE_OK_MSG : Code.UPDATE_ERROR_MSG);
    }


    @GetMapping("")
    @ApiOperation("排位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "排位名次", required = false, dataType = "String"),
            @ApiImplicitParam(name = "year", value = "排位年份", required = false, dataType = "String")
    })
    public Result listQueryRanking(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "year", required = false) String year) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(year), "DATE_FORMAT(ranking_date,'%Y')", year);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByDesc("ranking_date", "id");
        List<StaffRanking> staffRankingList = iStaffRankingService.list(queryWrapper);
        return new Result(Code.GET_OK, staffRankingList, Code.GET_OK_MSG);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除排位")
    public Result delete(@PathVariable(value = "id") String id){
        StaffRanking staffRanking = iStaffRankingService.getById(id);
        if (staffRanking == null) {
            return new Result(Code.DELETE_ERROR, Code.DELETE_ERROR_MSG, "该排位不存在,删除失败");
        }
        boolean remove = iStaffRankingService.removeStaffRankingById(id);

        //删除排位后对应的排位详情也删除
        iStaffRankingDetailService.remove(new QueryWrapper<StaffRankingDetail>().eq("staff_ranking_id", id));

        return new Result(remove ? Code.DELETE_OK : Code.DELETE_ERROR, null, remove ? Code.DELETE_OK_MSG : Code.DELETE_ERROR_MSG);
    }
}
